﻿namespace Huayou.UI.WebControls
{
    using System;
    using System.Xml;

    public class MenuItem
    {
        private MenuGroup _ParentGroup;
        internal string ClientSideID;
        internal XmlElement ItemNode;
        internal WebMenu Menu;

        public MenuItem()
        {
        }

        public MenuItem(XmlElement node, MenuGroup parentGroup, WebMenu menu)
        {
            this.ItemNode = node;
            this._ParentGroup = parentGroup;
            this.Menu = menu;
            if (node.Attributes["ClientSideID"] != null)
            {
                this.ClientSideID = node.Attributes["ClientSideID"].Value;
            }
            if (this.ImageOver != null)
            {
                this.Menu.AddToImageList(this.ImageOver);
            }
            if (this.ImageDown != null)
            {
                this.Menu.AddToImageList(this.ImageDown);
            }
            if (this.LeftIconOver != null)
            {
                this.Menu.AddToImageList(this.LeftIconOver);
            }
            if (this.LeftIconDown != null)
            {
                this.Menu.AddToImageList(this.LeftIconDown);
            }
            if (this.RightIconOver != null)
            {
                this.Menu.AddToImageList(this.RightIconOver);
            }
            if (this.RightIconDown != null)
            {
                this.Menu.AddToImageList(this.RightIconDown);
            }
            if (((this.Menu.PostBackItemID != null) && (this.Menu.PostBackItemID == this.ID)) && this.CausesValidation)
            {
                this.Menu.PerformPageValidate = true;
            }
        }

        public MenuGroup AddSubGroup()
        {
            XmlElement newChild = this.ItemNode.OwnerDocument.CreateElement("MenuGroup");
            this.ItemNode.AppendChild(newChild);
            return new MenuGroup(newChild, this.Menu);
        }

        public void RemoveSubGroup()
        {
            if (this.ItemNode.HasChildNodes)
            {
                this.ItemNode.RemoveChild(this.ItemNode.ChildNodes[0]);
            }
        }

        public bool CausesValidation
        {
            get
            {
                if (this.ItemNode.Attributes["CausesValidation"] != null)
                {
                    return bool.Parse(this.ItemNode.Attributes["CausesValidation"].Value);
                }
                return this.Menu.CausesValidation;
            }
            set
            {
                this.ItemNode.SetAttribute("CausesValidation", value.ToString());
            }
        }

        public string ClientSideOnClick
        {
            get
            {
                if (this.ItemNode.Attributes["ClientSideOnClick"] != null)
                {
                    return this.ItemNode.Attributes["ClientSideOnClick"].Value;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("ClientSideOnClick", value);
            }
        }

        public string CssClass
        {
            get
            {
                if (this.ItemNode.Attributes["CssClass"] != null)
                {
                    return this.ItemNode.Attributes["CssClass"].Value;
                }
                if ((this.Image == null) && (this.Menu.DefaultItemCssClass != null))
                {
                    return this.Menu.DefaultItemCssClass;
                }
                if (this.Menu.useDefaultStyle)
                {
                    return "aspnmI";
                }
                return null;
            }
            set
            {
                if (value != "aspnmI")
                {
                    this.ItemNode.SetAttribute("CssClass", value);
                }
            }
        }

        public string CssClassDown
        {
            get
            {
                if (this.ItemNode.Attributes["CssClassDown"] != null)
                {
                    return this.ItemNode.Attributes["CssClassDown"].Value;
                }
                if ((this.Image == null) && (this.Menu.DefaultItemCssClassDown != null))
                {
                    return this.Menu.DefaultItemCssClassDown;
                }
                if (this.Menu.useDefaultStyle)
                {
                    return "aspnmID";
                }
                return null;
            }
            set
            {
                if (value != "aspnmID")
                {
                    this.ItemNode.SetAttribute("CssClassDown", value);
                }
            }
        }

        public string CssClassOver
        {
            get
            {
                if (this.ItemNode.Attributes["CssClassOver"] != null)
                {
                    return this.ItemNode.Attributes["CssClassOver"].Value;
                }
                if ((this.Image == null) && (this.Menu.DefaultItemCssClassOver != null))
                {
                    return this.Menu.DefaultItemCssClassOver;
                }
                if (this.Menu.useDefaultStyle)
                {
                    return "aspnmIO";
                }
                return null;
            }
            set
            {
                if (value != "aspnmIO")
                {
                    this.ItemNode.SetAttribute("CssClassOver", value);
                }
            }
        }

        public string Depart
        {
            get
            {
                if (this.ItemNode.Attributes["Depart"] != null)
                {
                    return this.ItemNode.Attributes["Depart"].Value;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("Depart", value);
            }
        }

        public string Height
        {
            get
            {
                if (this.ItemNode.Attributes["Height"] != null)
                {
                    return this.ItemNode.Attributes["Height"].Value;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("Height", value.ToString());
            }
        }

        public string ID
        {
            get
            {
                if (this.ItemNode.Attributes["ID"] != null)
                {
                    return this.ItemNode.Attributes["ID"].Value;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("ID", value);
            }
        }

        public string Image
        {
            get
            {
                if (this.ItemNode.Attributes["Image"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["Image"].Value);
                    }
                    return this.ItemNode.Attributes["Image"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("Image", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("Image", value);
                }
            }
        }

        public string ImageDown
        {
            get
            {
                if (this.ItemNode.Attributes["ImageDown"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["ImageDown"].Value);
                    }
                    return this.ItemNode.Attributes["ImageDown"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("ImageDown", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("ImageDown", value);
                }
            }
        }

        public string ImageOver
        {
            get
            {
                if (this.ItemNode.Attributes["ImageOver"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["ImageOver"].Value);
                    }
                    return this.ItemNode.Attributes["ImageOver"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("ImageOver", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("ImageOver", value);
                }
            }
        }

        public bool IsDisabled
        {
            get
            {
                return ((this.ItemNode.Attributes["IsDisabled"] != null) && bool.Parse(this.ItemNode.Attributes["IsDisabled"].Value));
            }
            set
            {
                this.ItemNode.SetAttribute("IsDisabled", value.ToString());
            }
        }

        public bool IsVisible
        {
            get
            {
                if (this.ItemNode.Attributes["IsVisible"] != null)
                {
                    return bool.Parse(this.ItemNode.Attributes["IsVisible"].Value);
                }
                return true;
            }
            set
            {
                this.ItemNode.SetAttribute("IsVisible", value.ToString());
            }
        }

        public string ItemType
        {
            get
            {
                if (this.ItemNode.Attributes["ItemType"] != null)
                {
                    return this.ItemNode.Attributes["ItemType"].Value.ToLower();
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("ItemType", value);
            }
        }

        public string Label
        {
            get
            {
                if (this.ItemNode.Attributes["Label"] != null)
                {
                    return this.ItemNode.Attributes["Label"].Value;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("Label", value);
            }
        }

        public string LeftIcon
        {
            get
            {
                if (this.ItemNode.Attributes["LeftIcon"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["LeftIcon"].Value);
                    }
                    return this.ItemNode.Attributes["LeftIcon"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("LeftIcon", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("LeftIcon", value);
                }
            }
        }

        public string LeftIconDown
        {
            get
            {
                if (this.ItemNode.Attributes["LeftIconDown"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["LeftIconDown"].Value);
                    }
                    return this.ItemNode.Attributes["LeftIconDown"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("LeftIconDown", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("LeftIconDown", value);
                }
            }
        }

        public int LeftIconHeight
        {
            get
            {
                if (this.ItemNode.Attributes["LeftIconHeight"] != null)
                {
                    return int.Parse(this.ItemNode.Attributes["LeftIconHeight"].Value);
                }
                return -1;
            }
            set
            {
                this.ItemNode.SetAttribute("LeftIconHeight", value.ToString());
            }
        }

        public string LeftIconOver
        {
            get
            {
                if (this.ItemNode.Attributes["LeftIconOver"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["LeftIconOver"].Value);
                    }
                    return this.ItemNode.Attributes["LeftIconOver"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("LeftIconOver", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("LeftIconOver", value);
                }
            }
        }

        public int LeftIconWidth
        {
            get
            {
                if (this.ItemNode.Attributes["LeftIconWidth"] != null)
                {
                    return int.Parse(this.ItemNode.Attributes["LeftIconWidth"].Value);
                }
                return -1;
            }
            set
            {
                this.ItemNode.SetAttribute("LeftIconWidth", value.ToString());
            }
        }

        public bool NoWrap
        {
            get
            {
                return ((this.ItemNode.Attributes["NoWrap"] != null) && bool.Parse(this.ItemNode.Attributes["NoWrap"].Value));
            }
            set
            {
                this.ItemNode.SetAttribute("NoWrap", value.ToString());
            }
        }

        public MenuGroup ParentGroup
        {
            get
            {
                return this._ParentGroup;
            }
        }

        public string RightIcon
        {
            get
            {
                if (this.ItemNode.Attributes["RightIcon"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["RightIcon"].Value);
                    }
                    return this.ItemNode.Attributes["RightIcon"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("RightIcon", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("RightIcon", value);
                }
            }
        }

        public string RightIconDown
        {
            get
            {
                if (this.ItemNode.Attributes["RightIconDown"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["RightIconDown"].Value);
                    }
                    return this.ItemNode.Attributes["RightIconDown"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("RightIconDown", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("RightIconDown", value);
                }
            }
        }

        public int RightIconHeight
        {
            get
            {
                if (this.ItemNode.Attributes["RightIconHeight"] != null)
                {
                    return int.Parse(this.ItemNode.Attributes["RightIconHeight"].Value);
                }
                return -1;
            }
            set
            {
                this.ItemNode.SetAttribute("RightIconHeight", value.ToString());
            }
        }

        public string RightIconOver
        {
            get
            {
                if (this.ItemNode.Attributes["RightIconOver"] != null)
                {
                    if (this.Menu.ImagesBaseURL != null)
                    {
                        return (this.Menu.ImagesBaseURL + this.ItemNode.Attributes["RightIconOver"].Value);
                    }
                    return this.ItemNode.Attributes["RightIconOver"].Value;
                }
                return null;
            }
            set
            {
                if (this.Menu.ImagesBaseURL != null)
                {
                    this.ItemNode.SetAttribute("RightIconOver", value.Replace(this.Menu.ImagesBaseURL, ""));
                }
                else
                {
                    this.ItemNode.SetAttribute("RightIconOver", value);
                }
            }
        }

        public int RightIconWidth
        {
            get
            {
                if (this.ItemNode.Attributes["RightIconWidth"] != null)
                {
                    return int.Parse(this.ItemNode.Attributes["RightIconWidth"].Value);
                }
                return -1;
            }
            set
            {
                this.ItemNode.SetAttribute("RightIconWidth", value.ToString());
            }
        }

        public MenuGroup SubGroup
        {
            get
            {
                if (this.ItemNode.HasChildNodes)
                {
                    return new MenuGroup((XmlElement) this.ItemNode.ChildNodes[0], this.Menu);
                }
                return null;
            }
        }

        public string TextAlign
        {
            get
            {
                if (this.ItemNode.Attributes["TextAlign"] != null)
                {
                    return this.ItemNode.Attributes["TextAlign"].Value;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("TextAlign", value);
            }
        }

        public string URL
        {
            get
            {
                if (this.ItemNode.Attributes["URL"] != null)
                {
                    return this.ItemNode.Attributes["URL"].Value;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("URL", value);
            }
        }

        public bool URLAbsolute
        {
            get
            {
                return ((this.ItemNode.Attributes["URLAbsolute"] != null) && bool.Parse(this.ItemNode.Attributes["URLAbsolute"].Value));
            }
            set
            {
                this.ItemNode.SetAttribute("URLAbsolute", value.ToString());
            }
        }

        public string URLTarget
        {
            get
            {
                if (this.ItemNode.Attributes["URLTarget"] != null)
                {
                    return this.ItemNode.Attributes["URLTarget"].Value;
                }
                if (this.Menu.DefaultTarget != null)
                {
                    return this.Menu.DefaultTarget;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("URLTarget", value);
            }
        }

        public string Width
        {
            get
            {
                if (this.ItemNode.Attributes["Width"] != null)
                {
                    return this.ItemNode.Attributes["Width"].Value;
                }
                return null;
            }
            set
            {
                this.ItemNode.SetAttribute("Width", value.ToString());
            }
        }
    }
}

